package com.chen.blue.controller.admin.system;

import com.chen.blue.domain.model.system.SysDictType;
import com.chen.blue.result.R;
import com.chen.blue.result.TableDataInfo;
import com.chen.blue.service.system.ISysDictTypeService;
import com.chen.blue.service.security.SecurityService;
import com.chen.blue.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

import static com.chen.blue.utils.TableDateUtil.getDataTable;


/**
 * 数据字典信息
 *
 * @author ruoyi
 */


@Slf4j
@RequestMapping("/system/dict/type")
@RestController
@Api(tags = "字典类型模块")
public class SysDictTypeController {

    @Resource
    private ISysDictTypeService dictTypeService;

    @ApiOperation("获取字典类型列表")
    @GetMapping("/list")
    public R<List<SysDictType>> list(SysDictType dictType) {

        return R.success(dictTypeService.selectDictTypeList(dictType));

    }

    @ApiOperation("导出字典类型数据")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictType dictType) throws IOException {

        List<SysDictType> list = (List<SysDictType>) dictTypeService.selectDictTypeList(dictType).getResult();
        ExcelUtil.export(response, SysDictType.class, String.valueOf(System.currentTimeMillis()), "字典类型", list);

    }

    /**
     * 查询字典类型详细
     */
    @ApiOperation("查询数据字典类型")
    @GetMapping(value = "/{dictId}")
    public R<SysDictType> getInfo(@PathVariable Long dictId) {

        return R.success(dictTypeService.selectDictTypeById(dictId));

    }

    /**
     * 新增字典类型
     */
    @ApiOperation("新增字典类型")
    @PostMapping
    public R add(@Validated @RequestBody SysDictType dict) {

        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return R.faild("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateBy(SecurityService.currentUserName());
        return (dictTypeService.insertDictType(dict)) > 0 ? R.success() : R.faild();

    }

    /**
     * 修改字典类型
     */
    @ApiOperation("修改字典类型")
    @PutMapping
    public R edit(@Validated @RequestBody SysDictType dict) {

        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return R.faild("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(SecurityService.currentUserName());
        return (dictTypeService.updateDictType(dict)) > 0 ? R.success() : R.faild();

    }

    /**
     * 删除字典类型
     */
    @ApiOperation("删除字典类型")
    @DeleteMapping("/{dictIds}")
    public R remove(@PathVariable Long[] dictIds) {

        int isSuccess = dictTypeService.deleteDictTypeByIds(dictIds);
        return isSuccess > 0 ? R.success() : R.faild();

    }

    /**
     * 获取字典选择框列表
     */
    @ApiOperation("获取字典选择框列表")
    @GetMapping("/optionselect")
    public R optionselect() {

        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return R.success(dictTypes);

    }
}
